package com.hotel.controller.htreservation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.dto.HtReservationDto;
import com.hotel.entity.HtReservation;
import com.hotel.entity.HtRoom;
import com.hotel.entity.HtRoomType;
import com.hotel.entity.HtStatus;
import com.hotel.service.htreservation.HtReservationService;
import com.hotel.service.htroom.HtRoomService;
import com.hotel.service.htroomtype.HtRoomTypeService;
import com.hotel.service.htstatus.HtStatusService;
import com.hotel.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RequestMapping("/Order")
@RestController
@CrossOrigin
@Slf4j
public class HtReservationController {

    @Resource
    private HtReservationService htReservationService;

    @Resource
    private HtStatusService htStatusService;

    @Resource
    private HtRoomTypeService htRoomTypeService;

    @Resource
    private HtRoomService htRoomService;

    /**
     * 客房预定分页查询(条件查询)
     *
     * @param htReservationDto
     * @return
     */
    @PostMapping("/list")
    public R getReservationDtolist(HtReservationDto htReservationDto) {
        try {
            //构造分页构造器对象
            Page<HtReservation> pageInfo = new Page<>(htReservationDto.getPage(), htReservationDto.getLimit());
            Page<HtReservationDto> htReservationDtoPage = new Page<>();

            LambdaQueryWrapper<HtReservation> htReservationlqw = new LambdaQueryWrapper<>();
            htReservationlqw.like(htReservationDto.getReservationNo() != null && htReservationDto.getReservationNo() != "", HtReservation::getReservationNo, htReservationDto.getReservationNo());
            htReservationlqw.like(htReservationDto.getPerson() != null && htReservationDto.getPerson() != "", HtReservation::getPerson, htReservationDto.getPerson());
            htReservationlqw.like(htReservationDto.getRoomNo() != null && htReservationDto.getRoomNo() != 0, HtReservation::getRoomNo, htReservationDto.getRoomNo());
            if (htReservationDto.getRoomTypeNo().equals("0")) {
                htReservationDto.setRoomTypeNo(null);
            }
            htReservationlqw.like(htReservationDto.getRoomTypeNo() != null && htReservationDto.getRoomTypeNo() != 0, HtReservation::getRoomTypeNo, htReservationDto.getRoomTypeNo());
            if (htReservationDto.getStatusName().equals("0")) {
                htReservationDto.setStatusNo("0");
            } else {
                htReservationDto.setStatusNo(htReservationDto.getStatusName());
            }
            htReservationlqw.like(htReservationDto.getStatusNo() != null && htReservationDto.getStatusNo() != "0", HtReservation::getStatusNo, htReservationDto.getStatusNo());

            htReservationService.page(pageInfo, htReservationlqw);

            //对象拷贝
            BeanUtils.copyProperties(pageInfo, htReservationDtoPage, "records");

            List<HtReservation> htReservationsList = pageInfo.getRecords();

            List<HtReservationDto> list = htReservationsList.stream().map((item) -> {

                HtReservationDto htReservationDto1 = new HtReservationDto();

                BeanUtils.copyProperties(item, htReservationDto1);

                String categoryId = item.getStatusNo();
                Long roomTypeNo = item.getRoomTypeNo();
                //根据id查询分类对象
                HtStatus htStatus = htStatusService.getById(categoryId);
                HtRoomType htRoomType = htRoomTypeService.getById(roomTypeNo);

                if (htStatus != null) {
                    String categoryName = htStatus.getStatusName();
                    htReservationDto1.setStatusName(categoryName);
                }
                if (htRoomType != null) {
                    String htRoomTypeName = htRoomType.getRoomTypeName();
                    htReservationDto1.setRoomTypeName(htRoomTypeName);
                }
                List<HtStatus> htStatusList = htStatusService.list();
                htStatusList.add(0, new HtStatus((long) 0, "请选择"));
                htReservationDto1.setHtStatusList(htStatusList);

                List<HtRoomType> htRooTypeList = htRoomTypeService.list();
                htRooTypeList.add(0, new HtRoomType((long) 0, "请选择"));
                htReservationDto1.setHtRoomTypeList(htRooTypeList);
                return htReservationDto1;
            }).collect(Collectors.toList());


            htReservationDtoPage.setRecords(list);

            return new R(R.OK, htReservationDtoPage);
        } catch (Exception e) {
            return new R(R.SERVER_ERROR);
        }
    }


    /**
     * 客房预定新增（修改）
     *
     * @param htReservation
     * @return
     */
    @PostMapping("/updeta")
    public R update(HtReservation htReservation) {
        LambdaQueryWrapper<HtStatus> statusLQW = new LambdaQueryWrapper<>();
        if (htReservation.getStatusName().length() > 1) {
            statusLQW.eq(HtStatus::getStatusName, htReservation.getStatusName());
        } else {
            statusLQW.eq(HtStatus::getStatusNo, htReservation.getStatusName());
        }
        HtStatus byId = htStatusService.getOne(statusLQW);
        htReservation.setStatusNo(byId.getStatusNo().toString());

        if (htReservation.getReservationRemark().equals("null")) {
            htReservation.setReservationRemark("");
        }

        LambdaQueryWrapper<HtRoom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtRoom::getRoomNo,htReservation.getRoomNo());
        HtRoom one = htRoomService.getOne(lqw);
        one.setStatusNo(htReservation.getStatusNo());

        if (htReservation.getReservationNo() == "") {
            if (one != null && one.getStatusNo().equals("3")) {
                return new R(R.SERVER_ERROR,"该房间已入住");
            }else if (one != null && one.getStatusNo().equals("1")){
                return new R(R.SERVER_ERROR,"该房间已预定");
            }else {
                htRoomService.updateById(one);
                htReservationService.save(htReservation);
            }
        } else {
            htRoomService.updateById(one);
            htReservationService.updateById(htReservation);
        }
        return new R(R.OK);
    }

    /**
     * 客房预定取消
     *
     * @param htReservation
     * @return
     */
    @PostMapping("/delete")
    public R delete(HtReservation htReservation) {
        HtReservation htReservation2 = htReservationService.getById(htReservation);
        htReservation2.setStatusNo("2");
        LambdaQueryWrapper<HtRoom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtRoom::getRoomNo,htReservation.getRoomNo());
        HtRoom htRoom = htRoomService.getOne(lqw);
        htRoom.setStatusNo("2");
        htRoomService.updateById(htRoom);
        htReservationService.updateById(htReservation2);
        return new R(R.OK);
    }
}
